#pragma  once
#include "..\RealTimeDataSet\RealTimeDataSet.h"
 
#include "base\at_exit.h"
#include "base\message_loop.h"
#include "base\location.h"
#include "base\bind.h"

 
namespace
{
	const static  int kUpdateDataCount = 1024*4 + 1;
	struct ArrayContainer
	{
	public:
		typedef  boost::shared_array<uint16>	IntArray;
		void  Push(IntArray  array)		{	array_list.push_back(array);	}

		uint16  GetAt(int  index)	
		{
			return array_list[index/kUpdateDataCount][index%kUpdateDataCount];
		}
	private:
		std::vector<IntArray>	array_list;
	};
}

class  RealTimeDataSetTest : public base::RefCounted<RealTimeDataSetTest>
{
public:
	virtual void SetUp(MessageLoopForIO *  io_loop, DataManager::RealTimeDataSet* set )
	{
		data_send_count = 0;
		data_kept_count = 0;
		data_observer = NULL;
		data_set =  set;
		counter_ = 0;
		message_loop = io_loop;
	}

	void  BeginCollectData( DataAcquisition::IChannelDataObserver *  observer)
	{
		data_observer = observer;
		message_loop->PostDelayedTask(FROM_HERE, base::Bind(&RealTimeDataSetTest::UpdateData, base::Unretained(this)),
			base::TimeDelta::FromMilliseconds(20)); 
	}

	void  UpdateData()
	{
		using  boost::shared_array;

		const static  int kStatisticDataCount = kUpdateDataCount/ (DataManager::kStatisticsGranularity) + 1;

		shared_array<uint16>	raw_data(new uint16[kUpdateDataCount]);
        for (uint16 i = 0; i < kUpdateDataCount; i++)
        {
            *(raw_data.get()+i) =  ++counter_;
        }
		raw_data_container.Push(raw_data);
		data_observer->OnRawDataUpdate(raw_data, kUpdateDataCount);

		shared_array<uint16>	min_data(new uint16[kStatisticDataCount]);
		data_observer->OnMinDataUpdate(min_data, kStatisticDataCount);

		shared_array<uint16>	max_data(new uint16[kStatisticDataCount]);
		data_observer->OnMaxDataUpdate(max_data, kStatisticDataCount);

		shared_array<uint16>	average_data(new uint16[kStatisticDataCount]);
		data_observer->OnAverageDataUpdate(average_data, kStatisticDataCount );

		data_send_count += kUpdateDataCount;
		if( data_kept_count>= 2 * DataManager::DataFrame::kDataFrameIndexCount)
			data_kept_count = DataManager::DataFrame::kDataFrameIndexCount ;
		data_kept_count += kUpdateDataCount;

	
		message_loop->PostDelayedTask(FROM_HERE, base::Bind(&RealTimeDataSetTest::UpdateData, base::Unretained(this)),
		base::TimeDelta::FromMilliseconds(20)); 
	}

protected:
	base::AtExitManager	at_exit;

	MessageLoopForIO	*		message_loop;
	DataAcquisition::IChannelDataObserver *	data_observer;
	scoped_refptr<DataManager::RealTimeDataSet> 	data_set;
	int					data_send_count;
	int					data_kept_count;

	int64				counter_ ;
	ArrayContainer		raw_data_container;
	const	static int  kDataCount =  0x1<<14 <<2;
};
 